<template>
  <el-dialog
    v-if="open"
    class="dia-style"
    :title="title"
    :visible.sync="open"
    append-to-body
    width="1000px"
    :close-on-click-modal="false"
    @close="cancel"
  >
    <el-form
      ref="form"
      :model="formData"
      label-width="140px"
      label-position="left"
      style="padding: 0 10px"
    >
      <!-- 基础信息部分 -->
      <div class="upload-title-box">
        <div class="upload-title">基础信息</div>
        <el-checkbox
          v-model="showRequired"
          @change="changeShow"
        >仅展示必填信息</el-checkbox>
      </div>
      <HkForm
        ref="hkFormRef1"
        :form-labels="basicArr"
        :rules="formRules"
        :data="formData"
        :dict-list="dictList"
        :btn-hidden="true"
        label-width="140"
        label-position="left"
      >
        <template #filter-equipmentOrgId>
          <treeselect v-model="formData.equipmentOrgId" noResultsText="无匹配数据"  :append-to-body="true" :options="dictList.orgList" :normalizer="normalizer" style="width: 100%;" placeholder="请选择责任部门" @select="changeOrg" />
        </template>
        <template #filter-equipmentUserId>
          <el-select v-model="formData.equipmentUserId" filterable placeholder="请选择" style="width: 100%;" :clearable="true">
            <el-option
              v-for="item in userListData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </template>
        <template #filter-equipmentType>
          <treeselect v-model="formData.equipmentType" noResultsText="无匹配数据"  :append-to-body="true" :options="dictList.secproductList" :normalizer="normalizer" style="width: 100%;" placeholder="选择类型" />
        </template>
        <template #filter-deployServer>
          <treeselect v-model="formData.deployServer" noResultsText="无匹配数据"  :append-to-body="true" :options="dictList.secproductList" :normalizer="normalizer" style="width: 100%;" placeholder="选择类型" />
        </template>
        <template #filter-loadBalancing>
          <treeselect v-model="formData.loadBalancing" noResultsText="无匹配数据"  :append-to-body="true" :options="dictList.secproductList" :normalizer="normalizer" style="width: 100%;" placeholder="选择类型" />
        </template>
        <template #filter-storageDevice>
          <treeselect v-model="formData.storageDevice" noResultsText="无匹配数据"  :append-to-body="true" :options="dictList.secproductList" :normalizer="normalizer" style="width: 100%;" placeholder="选择类型" />
        </template>
        <template #filter-middleware>
          <treeselect v-model="formData.middleware" noResultsText="无匹配数据"  :append-to-body="true" :options="dictList.secproductList" :normalizer="normalizer" style="width: 100%;" placeholder="选择类型" />
        </template>

      </HkForm>

      <!-- 增加表格 -->
      <div>
        <el-collapse v-model="activeNames">
          <el-collapse-item name="6">
            <span slot="title" class="collapse-title">端口及服务列表</span>
            <AddTable ref="add_table" :table-data="formData.portServerList" :table-list="portServerListTable" />
          </el-collapse-item>
          <el-collapse-item name="7">
            <span slot="title" class="collapse-title">业务页面地址列表</span>
            <AddTable ref="add_table" :table-data="formData.businessAddressList" :table-list="businessAddressListTable" />
          </el-collapse-item>
          <el-collapse-item name="8">
            <span slot="title" class="collapse-title">管理页面地址列表</span>
            <AddTable ref="add_table" :table-data="formData.manageAddressList" :table-list="manageAddressListTable" />
          </el-collapse-item>
          <el-collapse-item name="9">
            <span slot="title" class="collapse-title">角色及账号列表</span>
            <AddTable ref="add_table" :table-data="formData.accountList" :table-list="accountListTable" />
          </el-collapse-item>
        </el-collapse>

      </div>

      <div>
        <el-collapse v-model="activeNames">
          <el-collapse-item name="1">
            <span slot="title" class="collapse-title">重要程度</span>
            <HkForm
              ref="hkFormRef"
              :form-labels="criticalityArr"
              :rules="formRules"
              :data="formData"
              :dict-list="dictList"
              :btn-hidden="true"
              label-width="140"
              label-position="left"
            />
          </el-collapse-item>
          <el-collapse-item name="2">
            <span slot="title" class="collapse-title">定密信息</span>
            <HkForm
              ref="hkFormRef"
              :form-labels="secArr"
              :rules="formRules"
              :data="formData"
              :dict-list="dictList"
              :btn-hidden="true"
              label-width="140"
              label-position="left"
            />

          </el-collapse-item>

          <el-collapse-item name="3">
            <span slot="title" class="collapse-title">配置信息</span>
            <HkForm
              ref="hkFormRef"
              :form-labels="configArr"
              :rules="formRules"
              :data="formData"
              :dict-list="dictList"
              :btn-hidden="true"
              label-width="140"
              label-position="left"
            />
          </el-collapse-item>
          <el-collapse-item name="4">
            <span slot="title" class="collapse-title">维保信息</span>
            <HkForm
              ref="hkFormRef"
              :form-labels="maintenanceArr"
              :rules="formRules"
              :data="formData"
              :dict-list="dictList"
              :btn-hidden="true"
              label-width="140"
              label-position="left"
            />
          </el-collapse-item>
          <el-collapse-item name="5">
            <span slot="title" class="collapse-title">扩展信息</span>
            <el-row :gutter="20" class="iRow">
              <el-col :span="24">
                <el-form-item label="备注">
                  <el-input
                    v-model="formData.equipmentRemark"
                    type="textarea"
                    maxlength="250"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>

        </el-collapse>

      </div>

    </el-form>
    <div v-if="subBtn" slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import HkForm from '@/components/Hk-Form'
import {
  addDatas,
  editDatas,
  queryById,
  getNo,
  queryUserList
} from '@/api/assets/softwareSystem.js'
import AddTable from '@/components/Common/addTable.vue'
import Treeselect from '@riophae/vue-treeselect'

export default {
  components: { HkForm, AddTable, Treeselect },
  props: {
    // 标题
    title: {
      type: String,
      default: ''
    },
    dictList: {
      type: Object,
      default: () => {}
    },
    subBtn: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      // 对话框是否显示
      open: false,
      // 仅展示必填信息
      showRequired: false,
      // 折叠菜单展示的数据
      activeNames: [],
      // 表单数据
      formData: {},
      defaultFormData: {
        // 角色及账号列表
        accountList: [],
        // 业务页面地址列表
        businessAddressList: [],
        // 管理页面地址列表
        manageAddressList: [],
        // 端口及服务列表
        portServerList: [],
        equipmentNo: ''
      },
      // 人员信息
      userListData: [],
      // 基础信息
      basicArr: [
        {
          label: '名称',
          field: 'name',
          type: 'input',
          maxlength: 50
        },
        {
          label: '编号',
          field: 'equipmentNo',
          type: 'input',
          maxlength: 50
        },
        {
          label: '类型',
          field: 'equipmentType',
          type: 'slot'
        },
        {
          label: '是否国产化',
          field: 'equipmentLocalized',
          type: 'select',
          list: 'equipmentLocalizedList'
        },
        {
          label: '责任部门',
          field: 'equipmentOrgId',
          type: 'slot'
        },
        {
          label: '责任人',
          field: 'equipmentUserId',
          type: 'slot'
        },
        {
          label: '联网类型',
          field: 'equipmentHostNetType',
          type: 'select',
          list: 'equipmentHostNetTypeList'
        },
        {
          label: '使用状态',
          field: 'equipmentUseStatus',
          type: 'select',
          list: 'equipmentUseStatusList'
        },
        {
          label: '设备状态',
          field: 'equipmentStatus',
          type: 'select',
          list: 'equipmentStatusList'
        },
        {
          label: '涉密等级',
          field: 'equipmentSecLevel',
          type: 'select',
          list: 'equipmentSecLevelList'
        },
        {
          label: '所在安全域',
          field: 'equipmentDomainId',
          type: 'select',
          list: 'assetsSecDomainAutoList'
        },
        {
          label: '部署服务器',
          field: 'deployServer',
          type: 'select',
          list: 'serverList'

        },
        {
          label: '负载均衡设备',
          field: 'loadBalancing',
          type: 'select',
          list: 'loadBalancingList',
          hidden: false
        },
        {
          label: '存储设备',
          field: 'storageDevice',
          type: 'select',
          list: 'storageDeviceList',
          hidden: false
        },
        {
          label: '中间件',
          field: 'middleware',
          type: 'select',
          list: 'middleWareList',
          hidden: false
        },
        {
          label: '数据库',
          field: 'databaseUsed',
          type: 'select',
          list: 'dataBaseInfoList',
          hidden: false
        }
      ],
      // 重要程度
      criticalityArr: [
        {
          label: '重要程度',
          field: 'equipmentCriticalityLevel',
          type: 'select',
          list: 'equipmentCriticalityLevelList',
          span: 12
        },
        {
          label: '是否重点关注',
          field: 'equipmentFocusLevel',
          type: 'select',
          list: 'equipmentLocalizedList'
        }
      ],
      // 定密信息
      secArr: [
        {
          label: '定密时间',
          field: 'equipmentSecDate',
          type: 'datePicker',
          format: 'yyyy-MM-dd'
        },
        {
          label: '有无密级调整',
          field: 'equipmentSeclevelAdjust',
          type: 'select',
          list: 'equipmentSecLevelAdjustList'
        }
      ],
      // 配置信息
      configArr: [
        {
          label: '厂商',
          field: 'equipmentManufacturer',
          type: 'input',
          maxlength: 50
        },
        {
          label: '品牌',
          field: 'equipmentBrand',
          type: 'input',
          maxlength: 50
        },
        {
          label: '型号/版本',
          field: 'equipmentModel',
          type: 'input',
          maxlength: 50
        },
        {
          label: '配置概述',
          field: 'equipmentOverview',
          type: 'input',
          maxlength: 50
        },
        {
          label: '用途说明',
          field: 'equipmentPurpose',
          type: 'select',
          list: 'equipmentPurposeList'
        },
        {
          label: '存放位置',
          field: 'equipmentStorage',
          type: 'input',
          maxlength: 50
        }
      ],
      // 维保信息
      maintenanceArr: [
        {
          label: '购置时间',
          field: 'equipmentPurchaseTime',
          type: 'datePicker',
          format: 'yyyy-MM-dd'
        },
        {
          label: '入网时间',
          field: 'equipmentEnableTime',
          type: 'datePicker',
          format: 'yyyy-MM-dd'
        },
        {
          label: '过保日期',
          field: 'equipmentOverdueTime',
          type: 'datePicker',
          format: 'yyyy-MM-dd'
        },
        {
          label: '维保厂家名称',
          field: 'equipmentManufacturerName',
          type: 'input',
          maxlength: 50
        },
        {
          label: '维保厂家电话',
          field: 'equipmentManufacturerMobile',
          type: 'input',
          maxlength: 50
        },
        {
          label: '维保记录',
          field: 'equipmentManufacturerRecord',
          type: 'textarea',
          maxlength: 250,
          span: 24
        }
      ],
      // 表单校验
      formRules: {
        name: [{ required: true, message: '不能为空', trigger: 'blur' }],
        equipmentNo: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { validator: this.validateNo, trigger: 'blur' }
        ],
        equipmentType: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        equipmentLocalized: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        equipmentHostNetType: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],

        equipmentUserId: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        equipmentOrgId: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        equipmentUseStatus: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        equipmentStatus: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        equipmentSecLevel: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        deployServer: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        equipmentDomainId: [
          { required: true, message: '不能为空', trigger: 'change' }
        ]
      },
      // 端口及服务列表
      portServerListTable: [
        { label: '端口号', param: 'port', type: 'input', hidden: true, maxlength: 100 },
        { label: '协议', param: 'protocol', type: 'input', hidden: true, maxlength: 100 },
        { label: '应用服务名称', param: 'serverName', type: 'input', hidden: true, maxlength: 100 }
      ],
      // 业务页面地址列表
      businessAddressListTable: [
        { label: '页面名称', param: 'address', type: 'input', hidden: true, maxlength: 100 },
        { label: 'URL', param: 'url', type: 'input', hidden: true, maxlength: 100 }
      ],
      // 管理页面地址列表
      manageAddressListTable: [
        { label: '页面名称', param: 'address', type: 'input', hidden: true, maxlength: 100 },
        { label: 'URL', param: 'url', type: 'input', hidden: true, maxlength: 100 }
      ],
      // 端口及服务列表
      accountListTable: [
        { label: '角色', param: 'role', type: 'input', hidden: true, maxlength: 100 },
        { label: '账号', param: 'account', type: 'input', hidden: true, maxlength: 100 }
      ]

    }
  },
  computed: {},
  watch: {},

  mounted() {

  },
  methods: {
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    // 是否只展示必填项
    changeShow() {
      if (this.showRequired) {
        this.activeNames = []
      } else {
        this.activeNames = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
      }
      this.basicArr.forEach((item) => {
        if (typeof item.hidden === 'boolean') {
          item.hidden = this.showRequired
        }
      })
    },
    // 清空表单
    resetFields() {
      this.formData = {}
      this.$refs['form'].resetFields()
      this.$refs['hkFormRef1']?.resetForm('formLabels')
      this.$refs['hkFormRef']?.resetForm('formLabels')
    },
    show(id) {
      this.showRequired = true
      this.changeShow()
      return new Promise((resolve, reject) => {
        if (id) {
          queryById(id).then((res) => {
            this.formData = res
            this.getUserList(res.equipmentOrgId)
          })
        } else {
          getNo().then((res) => {
            this.formData = JSON.parse(JSON.stringify(this.defaultFormData))
            this.formData.equipmentNo = res
          })
        }
        resolve(this.formData)
      }).then((res) => {
        this.open = true
      })
    },
    // 提交事件
    async submitForm() {
      const res = await this.$refs.hkFormRef1.handleFormSubmit('formLabels')
      if (this.title == '添加') {
        addDatas(this.formData).then((res) => {
          this.$message.success('新增成功')
          this.cancel()
          this.$emit('handleAdd')
        })
      } else {
        editDatas(this.formData).then((res) => {
          this.$message.success('修改成功')
          this.cancel()
          this.$emit('handleAdd')
        })
      }
    },
    // 关闭按钮
    cancel() {
      this.formData = { // 角色及账号列表
        accountList: [],
        // 业务页面地址列表
        businessAddressList: [],
        // 管理页面地址列表
        manageAddressList: [],
        // 端口及服务列表
        portServerList: [],
        equipmentNo: '' }
      this.userListData = []
      this.open = false
    },
    // 部门change事件
    changeOrg(node) {
      if (this.formData.equipmentUserId) {
        this.formData.equipmentUserId = ''
      }
      this.getUserList(node.id)
    },
    /** 查询列表 */
    getUserList(id) {
      queryUserList(id).then((res) => {
        this.userListData = res
      })
    },
    /** 验证编号重复
     * 是否合法No
     * @param value
     * @param callback
     */
    validateNo(rule, value, callback) {
      const that = this
      if (value !== '' && typeof value !== 'undefined' && value != null) {
        getNo(that.formData.id, value).then((res) => {
          if (res == 'false') {
            callback(new Error('该编号已存在'))
          } else {
            callback()
          }
        })
      }
    }

  }
}
</script>

<style lang="scss" scoped>
  .upload-title-box {
    height: 60px;
    width: 100%;
    padding: 0 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid rgba($color: #000000, $alpha: 8%);
    .upload-title {
      font-size: 16px;
      font-weight: 700;
      color: #000000;
    }
  }
  ::v-deep {
    .el-collapse-item {
      border-bottom: 1px solid #e6ebf5;
      .el-collapse-item__wrap {
        border-bottom: none;
      }
      .el-collapse-item__content {
        padding-bottom: 0px;
      }
    }
    .iRow {
      margin: 0 !important ;
    }
    .el-dialog__body {

      display: flex;
      flex-direction: column;
    }
    .el-collapse {
      border-top: none;
      .el-collapse-item__header {
        height: 60px;
        font-size: 16px;
        font-weight: 700;
        color: #000000;
        //margin-left: 30px;
        border-bottom: none;
        flex: 1 0 auto;
        order: -1;
      }
      .el-collapse-item__arrow {
        // margin:10px auto 0 10px;

        //margin-left:-90px;
      }

    .el-collapse-item__content{
      .el-row{
        width: 100%;
      }
    }
    }
  }
  .collapse-title {
    flex: 1 0 90%;
    order: 1;
  }

  .el-collapse-item__header {
    flex: 1 0 auto;
    order: -1;
  }

</style>
